package com.teachoa.bean;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.teachoa.constant.Global;
import com.teachoa.enumtype.UserTypeEnum;
import com.teachoa.util.IDUtil;
import com.teachoa.util.L;
import com.teachoa.util.Lists;
import com.teachoa.util.MD5Util;
import com.teachoa.util.Maps;

@SuppressWarnings({"rawtypes", "unchecked"})
public class User extends Model<User> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -883535354303177834L;
	
	public static final User dao = new User();
	
	public User checkUser(String username, String password) {
		String sql = "select * from user where loginname = ? and password = ?";
		return dao.findFirst(sql, new Object[]{username, password});
	}
	
	public Page<User> findUserByPage(Map<String, String> param) {
		String condition = "where 1=1 ";
		String username = param.get("username");
		List<String> list = Lists.newArrayList();
		if(StringUtils.isNotBlank(username)) {
			condition += "and username like ? ";
			list.add("%"+username+"%");
		}
		String suffix = "from user "+condition+" order by create_time desc";
		int pageNo = L.toInt(param.get("pageNo"));
		pageNo = pageNo==0?1:pageNo;
		return dao.paginate(pageNo, Global.pageSize, "select *", suffix, list.toArray());
	}
	
	public User findUserByUserName(String name) {
		return dao.findFirst("select * from user where username = ?", new Object[]{name});
	}
	
	public Page<User> getUserPageByType(int pageNo, int type) {
		return dao.paginate(pageNo, Global.pageSize, "select *", "from user where type = ?"
				+ " order by create_time desc", new Object[]{type});
	}
	
	public Page<User> getStudentsByTeacherId(int pageNo, String teacherId) {
		return dao.paginate(pageNo, Global.pageSize, "select a.*", " from user a left join teacher_student b on a.id = b.student_id "
				+ "where b.teacher_id = ?", new Object[]{teacherId});
	}
	
	public Page<User> selectStudentsByCondition(Map<String, String> param) {
		int pageNo = L.toInt(param.get("pageNo"));
		String suffix = " from user where type = 2 ";
		String grade = param.get("grade");
		String major = param.get("major");
		List list = Lists.newArrayList();
		if(StringUtils.isNotEmpty(grade)) {
			suffix += " and grade = ?";
			list.add(Integer.parseInt(grade));
		}
		if(StringUtils.isNotEmpty(major)) {
			suffix += " and major = ?";
			list.add(Integer.parseInt(major));
		}
		return dao.paginate(pageNo, Global.pageSize, "select *", suffix, list.toArray());
	}
	
	public List<User> findUserByType(int type) {
		return dao.find("select * from user where type = ?", type);
	}
	
	public List<User> getTeachersByStudentId(String id) {
		return dao.find("select a.* from user a left join teacher_student b on a.id = b.teacher_id where b.student_id = ?", id);
	}
	
	public void createUser() {
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "学生1", "type", UserTypeEnum.STUDENT.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "xuesheng1"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "学生2", "type", UserTypeEnum.STUDENT.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "xuesheng2"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "学生3", "type", UserTypeEnum.STUDENT.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "xuesheng3"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "学生4", "type", UserTypeEnum.STUDENT.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "xuesheng4"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师1", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi1"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师2", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi2"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师3", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi3"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师4", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi4"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师5", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi5"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师6", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi6"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师7", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi7"));
//		dao.save();
//		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师8", "type", UserTypeEnum.TEACHER.getCode()
//				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi8"));
//		dao.save();
		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "专家", "type", UserTypeEnum.EXPERT.getCode()
				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "zhuanjia"));
		dao.save();
		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "学生", "type", UserTypeEnum.STUDENT.getCode()
				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "xuesheng"));
		dao.save();
		dao.setAttrs(Maps.newHashMap("id", IDUtil.getId(), "username", "老师", "type", UserTypeEnum.TEACHER.getCode()
				, "password", MD5Util.md5("123456"), "create_time", new Date(), "loginname", "laoshi"));
		dao.save();
	}

}
